// 主界面
;define(function(require, exports, module) {
    'use strict';
    var util = require("util");
    var base = require("base");
    var validate = require("validate");
    var lock = false;
    var css_news = require("./newsDetail.css");
    var databind = require("databind");
    var newsCategoryIdDic = {
    	enabled:0,
    	newsCategoryId:0
    };
    var images=[];
    var contents=[];
    var newsId;
	var index = 2;    
    var init = function(page){
    	newsId=page.params.newsId;
    	$("#newsDetailNewsId").val(newsId);
    	inquireNewsCategoryPageList();
		initEvent();
    };
	var initEvent = function(){
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
        
        $("#main").on("click", "#recommend", function(){
        	recommendNews();
        });
        
        $("#main").on("click", "#cancelReommend", function(){
        	disableRecommendNews();
        });
    };
    
    var loadData = function(){
    	databind.fillInput("#newsDetail", {action:"inquireNewsDetail.json",version:"v1.0"});
    	databind.disableInput("#newsDetail");
    }
    
    //上传图片
	var uploadPicture = function(fileElementId) {
		$.ajaxFileUpload({
			url: "/finance/api/v1.0/uploadFile.json", //你处理上传文件的服务端
			secureuri: false,
			fileElementId: fileElementId,
			dataType: 'json',
			success: function(data) {
				$("#"+fileElementId).parent().find("label").css("background", "url(/finance/api/v1.0/downloadFile.file?fileUuid="+data.data[0].fileUuid+")no-repeat");
				$("#"+fileElementId).parent().find("label").attr("fileUuid", data.data[0].fileUuid);
			},
			complete: function(xml) {
				$("#newsFile").unbind().on('change', function() {
					uploadPicture();
				});
			},
			error: function(data, status, e) {
				if (data.msg == null) {
					base.alert(2, "文件太大无法上传");
				} else {
					base.alert(2, data.msg);
				}
			}
		});
	};
	
	//删除图片
	var deletePicture = function(fileUuid) {
		$.ajaxFileUpload({
			url: "/finance/api/v1.0/deleteFile.json?fileUuid="+fileUuid, //你处理上传文件的服务端
			secureuri: false,
			dataType: 'json',
			success: function(data) {
			},
			complete: function(xml) {
			},
			error: function(data, status, e) {
				base.alert(2, "参数错误");
			}
		});
	};
	
	// 推荐资讯
	var recommendNews = function() {
		base.fnAjax({
			action: "recommendNews.json", //接口名称
			oData: {
				newsId: newsId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.openUrl("news.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	// 取消推荐资讯
	var disableRecommendNews = function() {
		base.fnAjax({
			action: "disableRecommendNews.json", //接口名称
			oData: {
				newsId: newsId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					base.openUrl("news.html");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
    
    // 获取资讯栏目
	var inquireNewsCategoryPageList = function() {
		base.fnAjax({
			action: "inquireNewsCategoryPageList.json", //接口名称
			oData: {
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
					var tpl = $("#categoryDetailTpl").html();
					var html = juicer(tpl, data);
					$("#newsCategoryDetailId").html(html);
					loadData();
					$("#newsCategoryDetailId option[value="+$("#newsCategoryId").val()+"]").attr("selected","selected");
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
	
	// 获取资讯栏目
	var inquireNewsDetail = function() {
		base.fnAjax({
			action: "inquireNewsDetail.json", //接口名称
			oData: {
				newsId: newsId
			}, //传递参数
			fnSuccess: function(data) { //成功回调
				if (data.status == 200) {
				} else {
					base.alert(3, data.message);
				}
			}
		});
	};
    
    //新增新闻
    var createNews = function(){
    	var newsSourceLink=$("#newsSourceLink").val();//原文链接
    	var newsSource=$("#newsSource").val();//资讯来源
    	var imageUuid=$("#newsFileshort").attr("fileUuid");//资讯列表缩略图
    	var recommend=$(".recommend:checked").val();//是否推荐 推荐1
    	var summary=$("#summary").val();//资讯摘要
    	var newsTitle=$("#newsTitle").val();//资讯标题
    	var newsCategoryId=$("#newsCategoryId").val();//栏目ID
    	var newsItems = $(".newsItems");
    	
    	for(var j=0;j<newsItems.length;j++){
    		images.push($(newsItems[j]).find("label").attr("fileUuid"));
    		contents.push($(newsItems[j]).find("textarea").val());
    	}
    	
    	base.fnAjax({
            action: "createNews.json", //接口名称
            oData: {
                fileUuid: images,
                content: contents,
                newsSourceLink: newsSourceLink,
                imageUuid: imageUuid,
                recommend: recommend,
                summary: summary,
                newsTitle: newsTitle,
                newsCategoryId: newsCategoryId,
            }, //传递参数
            fnSuccess: function(data) { //成功回调
                if (data.status == 200) {
                	base.alert(1, "添加成功！");
                	base.openUrl("news.html");
                } else {
                    base.alert(3, data.message);
                }
            }
        });
    }
    
//  // 获取资讯栏目
//  var inquireNewsCategoryPageList = function(){
//  	 var html = function(data) {
//          var str = "";
//          for (var i = 0; i < data.datas.length; i++) {
//              var row = data.datas[i];
//              str += '<option value="' + row.newsCategoryId + '">' + row.categoryName + '</option>';
//          }
//          $("#newsCategoryId").append(str);
//          if (newsCategoryIdDic.enabled == 1) {
//          	$("#newsCategoryId").val(newsCategoryIdDic.newsCategoryId);	
//			}else{
//				newsCategoryIdDic.enabled = 1;
//			};
//      };
//      base.fnAjax({
//          action: "inquireNewsCategoryPageList.json", //接口名称
//          oData: {
//              categoryName: "",
//              pageIndex: pageIndex,
//              pageSize: pageSize
//          }, //传递参数
//          fnSuccess: function(data) { //成功回调
//              if (data.status == 200) {
//                  html(data);
//              } else {
//                  base.alert(3, data.message);
//              }
//          }
//      });
//  };
    
    return {
        init:init
    };
});